from multiprocessing import Process,Queue,Pool
import appFront
# from queue import Queue
import time
import flask_
from imgStruct import imgFrame

def get_gpu_id(port) -> int:
    if port in [5001, 5002, 5003]:
        return 0
    elif port in [5004, 5005]:
        return 1
    elif port in [5031, 5032, 5033]:
        return 2
    else:
        return 3

if __name__ == '__main__':
    port_list = [5001, 5002, 5003,
                 5004, 5005, 5031, 5032, 5033, 5034, 5035]
    # port_list = [5001]
    dict_ = {}
    gpu_id = 0
    process_list = []
    for port in port_list:
        gpu_id = get_gpu_id(port)
        dict_[str(port)] = imgFrame(str(port),gpu_id)
        process_list = []
        p = Process(target=flask_.start, args=(dict_,))
        process_list.append(p)

    for port in port_list:
        pp = Process(target=appFront.start, args=(dict_[str(port)],))
        process_list.append(pp)

    for process in process_list:
        process.start()
    for process in process_list:
        process.join()

    # dict_ = {
    #     "5001": imgFrame("5001",0),
    #     "5002": imgFrame("5002",0),
    #     "5003": imgFrame("5003",0),
    #     "5004": imgFrame("5004",1),
    #     "5005": imgFrame("5005",1),
    #     "5031": imgFrame("5031",2),
    #     "5032": imgFrame("5032",2),
    #     "5033": imgFrame("5033",2),
    #     "5034": imgFrame("5034",3),
    #     "5035": imgFrame("5035",3),
    # }

    # process_list = []
    # gpu_id =  0
    # for port in port_list:
    #     if port >=5034:
    #         gpu_id = 3
    #     elif port >= 5031:
    #         gpu_id = 2
    #     elif port >= 5004:
    #         gpu_id = 1
    #     dict_[str(port)] = imgFrame(str[port],gpu_id)
    # dict_['5001'] = [Queue() for _ in range(3)]
    # dict_['5002'] = [Queue() for _ in range(3)]
    # dict_['5003'] = [Queue() for _ in range(3)]
    # dict_['5004'] = [Queue() for _ in range(3)]
    # dict_['5005'] = [Queue() for _ in range(3)]
    # dict_['5031'] = [Queue() for _ in range(3)]
    # dict_['5032'] = [Queue() for _ in range(3)]
    # dict_['5033'] = [Queue() for _ in range(3)]
    # dict_['5034'] = [Queue() for _ in range(3)]
    # dict_['5035'] = [Queue() for _ in range(3)]

    # p1 = Process(target=appFront.start, args=(1,dict_["5001"]))
    # p2 = Process(target=appFront.start, args=(1,dict_["5002"]))
    # p3 = Process(target=appFront.start, args=(1,dict_["5003"]))
    # p4 = Process(target=appFront.start, args=(1,dict_["5004"]))
    # p5 = Process(target=appFront.start, args=(1,dict_["5005"]))
    # p6 = Process(target=appFront.start, args=(1,dict_["5031"]))
    # p7 = Process(target=appFront.start, args=(1,dict_["5032"]))
    # p8 = Process(target=appFront.start, args=(1,dict_["5033"]))
    # p9 = Process(target=appFront.start, args=(1,dict_["5034"]))
    # p10 = Process(target=appFront.start, args=(1,dict_["5035"]))
    # # p2 = Process(target=appFront.start, args=("5002", dict_['5002'],0))
    # # p3 = Process(target=appFront.start, args=("5003", dict_['5003'],0))
    # # p4 = Process(target=appFront.start, args=("5004", dict_['5004'],1))
    # # p5 = Process(target=appFront.start, args=("5005", dict_['5005'],1))
    # # p6 = Process(target=appFront.start, args=("5031", dict_['5031'],2))
    # # p7 = Process(target=appFront.start, args=("5032", dict_['5032'],2))
    # # p8 = Process(target=appFront.start, args=("5033", dict_['5033'],2))
    # # p9 = Process(target=appFront.start, args=("5034", dict_['5034'],3))
    # # p10 = Process(target=appFront.start, args=("5035", dict_['5035'],3))
    #
    # p = Process(target=flask_.start, args=(dict_,))
    # p.start()
    # p1.start()
    # p2.start()
    # p3.start()
    # p4.start()
    # p5.start()
    # p6.start()
    # p7.start()
    # p8.start()
    # p9.start()
    # p10.start()
    # # p2.start()
    # p1.join()
    # p2.join()
    # p3.join()
    # p4.join()
    # p5.join()
    # p6.join()
    # p7.join()
    # p8.join()
    # p9.join()
    # p10.join()
    # p.join()